package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.service.GoodsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("goodsType")
public class GoodsTypeController {

    @Autowired
    private GoodsTypeService goodsTypeService;

    /**
     * 遍历所有的商品类型
     * @return
     */
    @PostMapping("loadGoodsType")
    public String loadGoodsType(){

        return goodsTypeService.loadGoodsType();
    }

    /**
     * 添加分类信息
     * @param goodsTypeName
     * @param pId
     * @return
     */
    @PostMapping("save")
    public ServiceVO save(String goodsTypeName,Integer pId){
        Boolean flag = goodsTypeService.addGoodsType(goodsTypeName,pId);
        if (!flag){
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE,ErrorCode.PARA_TYPE_ERROR_MESS);
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    /**
     * 删除分类信息
     * @param goodsTypeId
     * @return
     */
    @PostMapping("delete")
    public ServiceVO delete(Integer goodsTypeId){
        Boolean flag = goodsTypeService.deleteById(goodsTypeId);
        if (flag){
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        }
        return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE,ErrorCode.PARA_TYPE_ERROR_MESS);

    }
}
